package com.jysz.certificate.web.action.cs;

import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.jysz.certificate.action.AbstractBaseAction;
import com.jysz.certificate.persistence.model.cs.ShowRow;
import com.jysz.certificate.persistence.model.cs.ShowTop;
import com.jysz.certificate.service.cs.CheckStockService;
import com.jysz.certificate.utils.PoiUtils;

/**
 * 盘库汇总查询
 * @author xuxiaolong
 */
@Controller
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Namespace("/cs")
public class CheckStockSearchAction extends AbstractBaseAction {

	private static final long serialVersionUID = -1115103002304945307L;
	
	@Resource
	private CheckStockService csService;
	
	private ShowTop top;
	private LinkedList<ShowRow> rowList;
	private LinkedHashMap<String,Integer> rowOne;
	private LinkedHashMap<String,Integer> rowTwo;
	private LinkedHashMap<String,Integer> rowThree;
	private List<Map<String, Object>> emptyList;
	
	private Date beginDate;//盘库日期-开始
	private Date endDate;//盘库日期-结束
	private Integer status;//已盘/未盘
	private Integer success;//成功/失败
	
	@Action("search")
	public String  search(){
		top=new ShowTop();
		rowList=new LinkedList<ShowRow>();
		if(beginDate!=null&&endDate!=null){
			if(status==null)
				status=-1;
			if(success==null)
				success=-1;
			if(status!=0||success==-1){//排除选择未盘 又选择成功或失败
				emptyList=csService.search(getUser(),top, rowList,beginDate,endDate,status,success);
			}
		}

		rowOne=top.getRowOne();
		rowTwo=top.getRowTwo();
		rowThree=top.getRowThree();
		return "search";
	}
	
	@Action("export")
	public String export() {
		top=new ShowTop();
		rowList=new LinkedList<ShowRow>();
		if(beginDate!=null&&endDate!=null){
			if(status==null)
				status=-1;
			if(success==null)
				success=-1;
			if(status!=0||success==-1)
				csService.search(getUser(),top, rowList,beginDate,endDate,status,success);
		}

		rowOne=top.getRowOne();
		rowTwo=top.getRowTwo();
		rowThree=top.getRowThree();

		String[] columns = {"#index","shopNum", "shopName", "shopUser", "areaManager" };
		HttpServletResponse response = this.getResponse();
	    response.setContentType("octets/stream");
	    response.addHeader("Content-Disposition",
	                "attachment;filename=" + toUtf8Encode("盘库汇总表") + ".xls");
		OutputStream out = null;
		try {
			
			out = this.getResponse().getOutputStream();
			Map<String, Object> context = new HashMap<String, Object>();
			context.put("cfa", this);
			PoiUtils.exportExcelCheckStock("盘库汇总表", rowOne,rowTwo,rowThree, columns, rowList, out, null, context,"cs");
		} catch (Exception e) {
			LOG.error("导出盘库汇总表出错。。。", e);
		}
		
		return null;
	}
	
	public ShowTop getTop() {
		return top;
	}
	public void setTop(ShowTop top) {
		this.top = top;
	}
	public LinkedList<ShowRow> getRowList() {
		return rowList;
	}
	public void setRowList(LinkedList<ShowRow> rowList) {
		this.rowList = rowList;
	}


	public LinkedHashMap<String, Integer> getRowOne() {
		return rowOne;
	}


	public void setRowOne(LinkedHashMap<String, Integer> rowOne) {
		this.rowOne = rowOne;
	}


	public LinkedHashMap<String, Integer> getRowTwo() {
		return rowTwo;
	}


	public void setRowTwo(LinkedHashMap<String, Integer> rowTwo) {
		this.rowTwo = rowTwo;
	}


	public LinkedHashMap<String, Integer> getRowThree() {
		return rowThree;
	}


	public void setRowThree(LinkedHashMap<String, Integer> rowThree) {
		this.rowThree = rowThree;
	}


	public Date getBeginDate() {
		return beginDate;
	}


	public void setBeginDate(Date beginDate) {
		this.beginDate = beginDate;
	}


	public Date getEndDate() {
		return endDate;
	}


	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public Integer getSuccess() {
		return success;
	}

	public void setSuccess(Integer success) {
		this.success = success;
	}

	public List<Map<String, Object>> getEmptyList() {
		return emptyList;
	}

	public void setEmptyList(List<Map<String, Object>> emptyList) {
		this.emptyList = emptyList;
	}

}
